import { Injectable } from '@angular/core';
import { ApolloQueryResult, FetchResult } from '@apollo/client/core';
import { Apollo, gql, QueryRef } from 'apollo-angular';
import { Observable } from 'rxjs';
import { UserServiceApi,
UserService_listResult,
UserService_listVariables,
UserService_totalCountResult,
UserService_searchResult,
UserService_searchVariables,
UserService_loadAllResult,
UserService_getByIdResult,
UserService_getByIdVariables,
UserService_createUserResult,
UserService_createUserVariables,
UserService_saveResult,
UserService_saveVariables,
UserService_removeByIdResult,
UserService_removeByIdVariables,
UserService_removeByIdListResult,
UserService_removeByIdListVariables,
TenantUserResultInfo, TenantUserPageInfo,
TenantUserConnection,
TenantUserPackage,
TenantUserOrderBy,
TenantUserMutationInfo,
TenantUserStringFilter,
TenantUserNumberFilter,
TenantUserBooleanFilter,
userServiceQuery,
userServiceMutation,
UserServiceQueryOptions,
TenantUserStore,
TenantUserView,
TenantUserInput,
TenantUserFilter,
TenantUserErrorBody} from '../model/user_service.model';

/************************************************************************
 * 用户管理
 */
@Injectable({
    providedIn: 'root'
})
export class UserService implements UserServiceApi {

        private listGql:string;
        private totalCountGql:string;
        private searchGql:string;
        private searchObjectRef: QueryRef<UserService_searchResult>;
        private loadAllGql:string;
        private getByIdGql:string;

    constructor(private apollo: Apollo) {
        this.listGql=`query aap_userService_list($filter: TenantUserFilter!,$orderBy: OrderBy){${userServiceQuery.userService_list}}`;
        this.totalCountGql=`query aap_userService_totalCount{${userServiceQuery.userService_totalCount}}`;
        this.searchGql=`query aap_userService_search($first: Int!,$offset: Float!,$filter: TenantUserFilter!,$orderBy: OrderBy){${userServiceQuery.userService_search}}`;
        this.searchObjectRef = apollo.watchQuery<UserService_searchResult>({ query: gql`${this.searchGql}`, fetchPolicy:'cache-first' });
        this.loadAllGql=`query aap_userService_loadAll{${userServiceQuery.userService_loadAll}}`;
        this.getByIdGql=`query aap_userService_getById($id: String!){${userServiceQuery.userService_getById}}`;
    }
    /**
     * 分页查询
     */
    list(variables: UserService_listVariables, options:UserServiceQueryOptions = { cachePolicy: 'no-cache', more: false }): Observable<ApolloQueryResult<UserService_listResult>> {
            return this.apollo.query({ query: gql`${this.listGql}`,variables ,fetchPolicy:options.cachePolicy});
    }
    /**
     * 统计库中所有对象的数据量
     */
    totalCount(options:UserServiceQueryOptions = { cachePolicy: 'no-cache', more: false }): Observable<ApolloQueryResult<UserService_totalCountResult>> {
            return this.apollo.query({ query: gql`${this.totalCountGql}`,fetchPolicy:options.cachePolicy});
    }
    /**
     * 游标查询
     */
    search(variables: UserService_searchVariables, options:UserServiceQueryOptions = { cachePolicy: 'cache-first', more: false }): Observable<ApolloQueryResult<UserService_searchResult>> {
        this.searchObjectRef.setVariables(variables);
        if(options.more){
            this.searchObjectRef.fetchMore({variables});
        }
        return this.searchObjectRef.valueChanges;
    }
    /**
     * 获取库中所有数据，数据量大时请谨慎使用
     */
    loadAll(options:UserServiceQueryOptions = { cachePolicy: 'cache-first', more: false }): Observable<ApolloQueryResult<UserService_loadAllResult>> {
            return this.apollo.query({ query: gql`${this.loadAllGql}`,fetchPolicy:options.cachePolicy});
    }
    /**
     * 获取一个对象
     */
    getById(variables: UserService_getByIdVariables, options:UserServiceQueryOptions = { cachePolicy: 'cache-first', more: false }): Observable<ApolloQueryResult<UserService_getByIdResult>> {
            return this.apollo.query({ query: gql`${this.getByIdGql}`,variables ,fetchPolicy:options.cachePolicy});
    }
    /**
     * 创建用户
     */
    createUser(variables: UserService_createUserVariables): Observable<FetchResult<UserService_createUserResult>>{
        return this.apollo.mutate({mutation: gql`mutation aap_userService_createUser($user: TenantUserInput!,$userTypes: [String]!,$tenant: String!,$password: String!){${userServiceMutation.userService_createUser}}`, variables,fetchPolicy:'no-cache'});
    }
    /**
     * 保存对象
     */
    save(variables: UserService_saveVariables): Observable<FetchResult<UserService_saveResult>>{
        return this.apollo.mutate({mutation: gql`mutation aap_userService_save($entity: TenantUserInput!){${userServiceMutation.userService_save}}`, variables,fetchPolicy:'no-cache'});
    }
    /**
     * 删除一个对象
     */
    removeById(variables: UserService_removeByIdVariables): Observable<FetchResult<UserService_removeByIdResult>>{
        return this.apollo.mutate({mutation: gql`mutation aap_userService_removeById($id: String!){${userServiceMutation.userService_removeById}}`, variables,fetchPolicy:'no-cache'});
    }
    /**
     * 删除一批对象
     */
    removeByIdList(variables: UserService_removeByIdListVariables): Observable<FetchResult<UserService_removeByIdListResult>>{
        return this.apollo.mutate({mutation: gql`mutation aap_userService_removeByIdList($ids: [String]!){${userServiceMutation.userService_removeByIdList}}`, variables,fetchPolicy:'no-cache'});
    }









}
